package at.socialconference.web.client.ui;

import java.util.List;

import at.socialconference.inter.model.Talk;
import at.socialconference.inter.model.User;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.i18n.client.DateTimeFormat.PredefinedFormat;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

public class ShowTalkPopup extends PopupPanel{
	public ShowTalkPopup(final Talk t) {
		this.setGlassEnabled(true);
		VerticalPanel vp = new VerticalPanel();
		FlexTable details = new FlexTable();
		this.add(vp);
		vp.add(details);
		int i = -1;
		details.setWidget(++i, 0, new Label("Talk:"));
		details.setWidget(i, 1, new Label(t.getTitle()));

		details.setWidget(++i, 0, new Label("Description:"));
		details.setWidget(i, 1, new Label(t.getDescription()));

		String date = DateTimeFormat.getFormat(PredefinedFormat.DATE_SHORT).format(t.getDate());
		String timeFrom = DateTimeFormat.getFormat(PredefinedFormat.TIME_SHORT).format(t.getFrom());
		String timeTo = DateTimeFormat.getFormat(PredefinedFormat.TIME_SHORT).format(t.getTo());

		details.setWidget(++i, 0, new Label("Date:"));
		details.setWidget(i, 1, new Label(date + " at " + timeFrom + " - " + timeTo));



		details.setWidget(++i, 0, new Label("Place:"));
		if(t.getPlace()!=null && t.getPlace().getLatitude()!=null&&
				t.getPlace().getLatitude()!=0.0&&t.getPlace().getLongitude()!=null&&t.getPlace().getLongitude()!=0.0){
			Button btn = new Button();
			btn.setText(t.getPlace().getName());
			btn.addClickHandler(new ClickHandler() {
				@Override
				public void onClick(ClickEvent event) {
					new MapPopUp(t.getPlace());
				}
			});
			details.setWidget(i, 1, btn);
		}else{
			details.setWidget(i, 1, new Label(t.getPlace()!=null?t.getPlace().getName():""));
		}


		String code = "003bb00"+t.getId();

		details.setWidget(++i, 0, new Label("QRCode:"));
		Image img = new Image("QRCodeServlet?message="+code);
		img.setPixelSize(125, 125);
		details.setWidget(i, 1, img);

		List<User> speakers = t.getSpeakers();

		if (speakers!=null && speakers.size()>0) {
			HorizontalPanel speakersPanel = new HorizontalPanel();
			for (User s : speakers) {
				speakersPanel.add(new LabelLinkedUser(s, false));
				speakersPanel.add(new Label(","));
			}
			//quickndirty
			speakersPanel.remove(speakersPanel.getWidgetCount()-1);

			details.setWidget(++i, 0, new Label("Speakers:"));
			details.setWidget(i, 1, speakersPanel);
		}
		
		this.center();
		HorizontalPanel hp = new HorizontalPanel();
		Button btnBack = new Button("Back");
		btnBack.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				ShowTalkPopup.this.hide();
			}
		});
		hp.add(btnBack);
		hp.setCellHorizontalAlignment(btnBack, HasHorizontalAlignment.ALIGN_CENTER);
		vp.add(hp);
	}
}
